import Vue from 'vue'
import Vuex from 'vuex'
import {constantRouterMap,asyncRouteMap} from '../router/index'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    routers: constantRouterMap,
    addRouters: [],
    operations: [],
    token: '',
  },
  mutations: {
    saveOpera(state, opera) {
      console.log('opera',opera)
      state.operations = opera;
    },
    saveToken(state, token) {
      state.token = token;
    },
    setRouters(state, routers) {
      state.addRouters = routers;
      state.routers[2].children=[];
      state.addRouters.map(e =>{
        state.routers[2].children.push(e);
      })
      console.log('state.routers',state.routers)
    }
  },
  actions: {
    GenerateRoutes({ commit }, data) {
      return new Promise(resolve => {
        const { operations } = data;
        console.log(operations,'operations')
        const accessedRouters = asyncRouteMap.filter(v => {
          for (let i = 0; i < operations.length; i++) {
            if (operations[i].path === v.path) return true;
          }
          return false;
        })
        console.log('accessedRouters',accessedRouters);
        commit('setRouters', accessedRouters);
        resolve();
      })
    }
  },
  modules: {
  }
})
